<template>
	<view class="content">
		<!-- 登录加载动画弹窗 -->
		<u-popup :show="showLoginLoading" :round="10" mode="center" :overlay="false" :closeable="false" :zoom="true"
			z-index="9999">
			<view class="loading-box">
				<u-loading-icon size="30" color="#4188EF" mode="circle"></u-loading-icon>
				<text class="loading-text">登录中...</text>
			</view>
		</u-popup>

		<view class="top">
			<image src="../../static/images/logo-font.png"></image>
		</view>
		<view class="title">守护您企业财税健康发展的每个日夜！</view>

		<view class="loginBtn" @click="login()">
			<image src="../../static/images/wx.png"></image>
			<view>微信一键登录</view>
		</view>

		<view class="treaty">
			<u-checkbox-group v-model="checkboxValue">
				<u-checkbox :customStyle="{marginRight: '8rpx'}" shape="circle" name="agreement"
					@change="checkboxChange">
				</u-checkbox>
			</u-checkbox-group>
			<text>我已认真阅读并同意</text>
			<span>用户协议</span>
			<text>与</text>
			<span>隐私条款</span>
		</view>

		<image class="img-bg" src="../../static/images/logo-blue.png"></image>
	</view>
</template>

<script>
	import {
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				socketTask: null,
				checkboxValue: [],
				// 登录加载弹窗控制
				showLoginLoading: false
			}
		},
		onLoad() {
			uni.setNavigationBarColor({
				frontColor: '#000000',
				backgroundColor: '#F5F5F5'
			})
		},
		methods: {
			...mapActions({
				wxLogin: 'user/wxLogin'
			}),
			async wxLoginHandle() {
				// 显示登录弹窗
				this.showLoginLoading = true

				try {
					const loginRes = await uni.login({
						"provider": "weixin",
						"onlyAuthorize": true
					})

					const {
						code
					} = loginRes
					await this.wxLogin(code)

					uni.setStorageSync('isLogin', true)
					uni.reLaunch({
						url: '/pages/index/index'
					})
				} catch (err) {
					console.log(err);
					uni.showToast({
						title: '登录失败，请重试',
						icon: 'error'
					})
				} finally {
					// 无论成功失败都关闭弹窗
					this.showLoginLoading = false
				}
			},
			sendMsg() {
				this.socketTask.send({
					data: "message",
					success(resp) {
						console.log(resp);
					}
				})
			},
			checkboxChange(ev) {
				this.checkboxValue = ev.value
			},
			login() {
				if (this.checkboxValue.length === 0) {
					uni.showToast({
						title: '请先阅读并勾选',
						icon: 'error'
					})
				} else {
					this.wxLoginHandle()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: calc(100% + 4rpx);
		height: calc(100vh - 88rpx);
		padding: 28rpx;
		margin-left: -30rpx;
		background: linear-gradient(180deg, rgba(65, 136, 239, 0.25) 0%, rgba(245, 249, 252, 0) 100%), rgba(245, 249, 252, 1) !important;

		.top {
			width: 100%;
			display: flex;
			justify-content: center;
			margin-top: 140rpx;

			image {
				width: 382rpx;
				height: 162rpx;
			}
		}

		.title {
			font-size: 26rpx;
			color: rgba(51, 51, 51, 1);
			text-align: center;
			margin-top: 8rpx;
			letter-spacing: 10rpx;
		}

		.loginBtn {
			width: 570rpx;
			height: 90rpx;
			font-size: 30rpx;
			color: #FFF;
			border-radius: 60rpx;
			background: #4188EF;
			box-shadow: 0rpx 2rpx 4rpx rgba(0, 0, 0, 0.1);
			letter-spacing: 2rpx;
			margin: 375rpx auto 30rpx auto;
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 20rpx;
			}
		}

		.treaty {
			font-size: 24rpx;
			display: flex;
			letter-spacing: 2rpx;
			align-items: center;
			justify-content: center;

			text {
				color: rgba(172, 172, 172, 1);
			}

			span {
				color: #4188EF;
			}

			.u-checkbox {
				margin-right: 8rpx;
			}
		}

		.img-bg {
			width: 440rpx;
			height: 460rpx;
			position: absolute;
			bottom: 0;
			left: 0;
		}
	}

	/* 登录加载弹窗样式 */
	.loading-box {
		width: 200rpx;
		height: 200rpx;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.loading-text {
		font-size: 28rpx;
		color: #333;
		margin-top: 20rpx;
		letter-spacing: 2rpx;
	}
</style>